<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//权限管理
class Privs extends CommonController {

	function __construct(){
		
		parent::__construct();
		$this->load->model('Index/PrivsModel','CurModel');
		
	}
	
	
	//start
	/**
	* @name 获取列表
	* @param page ture char 页面
	* @param size ture char 数量
	* @return 成功返回1
	*/
	//end
	function get_list(){
		$user_id = $this->user_id;
		
		
		
		$res = $this->CurModel->getAll(array("parent_id"=>0));
		
		foreach($res as $key=>$val){
			$list = $this->CurModel->getAll(array("parent_id"=>$val['id']));
			foreach($list as $k=>$v){
				$list[$k][$v['code'].'1'] = 0;
				$list[$k][$v['code'].'2'] = 0;
			}
			
			
			$res[$key]['list']= $list;
		}
		
		$this->data_out($res);
	}
	
	
	//start
	/**
	* @name 保存权限
	* @return 成功返回1
	*/
	//end
	function save_priv(){
		
		
		$id = $this->input->post('id',null,0);
		$privs = $this->input->post('privs',null,'');
		$querys = $this->input->post('querys',null,'');
		$actions = $this->input->post('actions',null,'');
		
		$this->load->model('Index/PrivgroupModel','PrivgroupModel');
		$info = $this->PrivgroupModel->get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		//对权限值进行修改，ling添加
		$arr = explode(',',$privs);
		$res = array();
		foreach($arr as $k=>$v){
			if(strpos($v,'_') !== false){
				$res[] = explode('_',$v)[0];
				$res[] = $v;
			}else{
				$res[] = $v;
			}
		}
		$uni_arr = array_unique($res);
		$new_privs = implode(',',$uni_arr);
		

		$arr = array(
			'privs'=>$new_privs,
			'querys'=>$querys,
			'actions'=>$actions,
		);

		$this->PrivgroupModel->edit($arr,"id='{$id}'");

		//写入缓存
		$this->load->dmodel('priv_group','priv_group');
		$rel = $this->priv_group->getAll(array('is_enable'=>1));
		parent::cache_write('priv_cache',$rel);
		
		$this->add_log("设置权限组【{$id}】的权限",'priv');
		
		$this->data_out(1);
	}
	
}
